/*
 *
 * 入口模块
 *
 */


// 引入Framework7框架
var FW7 = require('Framework7');
var jPusher = require('jPusher');
var mobui = require('mobui');

// 实例化Framework7框架
window.F7 = new Framework7({
    modalTitle: "盔甲",
    modalButtonOk: "确定",
    modalButtonCancel: "取消",
    modalPreloaderTitle: "加载中…",
    pushState: true
});

// 定义类jQuery的操作DOM的对象为全局对象:$$
window.$$ = Dom7;

// Add views
var homeView = F7.addView('#homeView', {
    dynamicNavbar: true
});
var orderListView = F7.addView('#orderListView', {
    dynamicNavbar: true
});
var orderAddView = F7.addView('#orderAddView', {
    dynamicNavbar: true
});
var userCenterView = F7.addView('#userCenterView', {
    dynamicNavbar: true
});


// 业务js
var loginAction = require('./pages/login');
var homeAction = require("./pages/home");
var orderListAction = require("./pages/orderList");
var orderDetailAction = require("./pages/orderDetail");
var orderEditAction = require("./pages/orderEdit");
var aboutAction = require("./pages/about");


// 定义全局的app对象，方便项目级使用
window.app = {
    version: '3.0.0',
    setRem: function() {
        var docEl = document.documentElement,
            resizeEvt = 'orientationchange' in window ? 'orientationchange' : 'resize',
            recalc = function() {
                var clientWidth = docEl.clientWidth;

                $('body').css('min-height',$(window).height());

                if (!clientWidth) return;
                docEl.style.fontSize = 20 * (clientWidth / 375) + 'px';
                // console.log(clientWidth)
            };
        if (!document.addEventListener) return;
        window.addEventListener(resizeEvt, recalc, false);

        recalc();
    },
    init: function() {
        // 设置REM
        this.setRem();
        loginAction.init();

        var isLogin = loginAction.checkLogin();

        if (isLogin) homeAction.init();

        this.indexView();
        this.foldCard();

        // Show/hide preloader for remote ajax loaded pages
        // Probably should be removed on a production/local app
        $$(document).on('ajaxStart', function(e) {
            if (e.detail.xhr.requestUrl.indexOf('autocomplete-languages.json') >= 0) {
                // Don't show preloader for autocomplete demo requests
                return;
            }
            F7.showIndicator();
        });
        $$(document).on('ajaxComplete', function(e) {
            if (e.detail.xhr.requestUrl.indexOf('autocomplete-languages.json') >= 0) {
                // Don't show preloader for autocomplete demo requests
                return;
            }
            F7.hideIndicator();
        });
    },
    // 封装的模板使用方法
    tplManager: function(params) {
        var appendBln = typeof params.isAppend === 'boolean' && params.isAppend,
            tplEl = params.tplEl,
            renderEl = params.renderEl,
            renderData = params.renderData,
            callback = params.callback;
            
        var tplHtml = $$(tplEl).html();
        var tpl = Template7.compile(tplHtml);
        var html = tpl(renderData);
        console.log(appendBln);
        if (renderEl) {
            if (appendBln) {
                $$(renderEl).append(html);
            } else {
                $$(renderEl).html(html);
            }
        }
        if (typeof callback == "function") callback();

        return html;
    },
    chkUpdate: function() {
        var appUpdate = cordova.require('cordova-plugin-app-update.AppUpdate'),
            updateUrl = "http://www.52v56.com/newApp/kuijiaSJ/version.xml",
            onSuccess = function() {
                console.log('获取版本信息成功!');
                var versionCode = AppVersion.build;
                console.log("版本号：" + versionCode);
            },
            onFail = function() {
                console.log('获取版本信息失败!');
            };


        appUpdate.checkAppUpdate(onSuccess, onFail, updateUrl);
    },
    backBtn: function() {
        var currentView = sessionStorage.getItem('current_page'),
            currentPageArr = ['home', 'mission', 'ship', 'unship'];

        currentView = currentView ? currentView : "home";

        if (currentPageArr.indexOf(currentView) != -1) {

            $.toast("再点击一次退出!");
            document.removeEventListener("backbutton", app.backBtn, false); // 注销返回键
            document.addEventListener("backbutton", app.exitApp, false);

            var intervalID = window.setInterval(function() {
                window.clearInterval(intervalID);
                document.removeEventListener("backbutton", app.exitApp, false); // 注销返回键  
                document.addEventListener("backbutton", app.backBtn, false); // 返回键  
            }, 2000);

            return false;

        } else {
            history.back();
        }

    },
    exitApp: function() {
        if (!localStorage.getItem("loginName")) window.plugins.jPushPlugin.stopPush();
        navigator.app.exitApp();
    },
    indexView: function() {
        $$('#homeView').on('show', function() {
            homeAction.init();
        });

        $$('#orderListView').on('show', function() {
            orderListAction.init();
        });

        $$('#orderAddView').on('show', function() {
            orderEditAction.init();
        });

        $$('#userCenterView').on('show', function() {

        });
    },
    foldCard: function() {
        $(document).on('click', '.fold-card-head', function() {
            var self = $(this),
                foldCard = self.parent();
            foldCard.toggleClass('off');
        });
    }
};

app.init();
